package cn.com.css.taxInspection.workflow.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import cn.com.css.taxInspection.system.entity.SysDictDetailEntity;
import cn.com.css.taxInspection.common.result.DataResult;
import cn.com.css.taxInspection.system.service.SysDictDetailService;
import cn.com.css.taxInspection.workflow.entity.WorkflowThresholdDetailEntity;
import cn.com.css.taxInspection.workflow.service.WorkflowThresholdDetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


/**
 * 阈值明细管理
 *
 * @author wenbin
 * @version V1.0
 * @date 2020年3月18日
 */
@Api(tags = "阈值明细管理")
@RestController
@RequestMapping("/workflow/thresholddetail")
public class WorkflowThresholdDetailController {
    @Resource
    private WorkflowThresholdDetailService workflowThresholdDetailService;

    @ApiOperation(value = "新增")
    @PostMapping("/add")
    @RequiresPermissions("workflow:threshold:add")
    public DataResult add(@RequestBody WorkflowThresholdDetailEntity sysDictDetail) {
        if (StringUtils.isEmpty(sysDictDetail.getValue())) {
            return DataResult.fail("阈值值不能为空");
        }
        LambdaQueryWrapper<WorkflowThresholdDetailEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(WorkflowThresholdDetailEntity::getValue, sysDictDetail.getValue());
        queryWrapper.eq(WorkflowThresholdDetailEntity::getThresholdId, sysDictDetail.getThresholdId());
        WorkflowThresholdDetailEntity q = workflowThresholdDetailService.getOne(queryWrapper);
        if (q != null) {
            return DataResult.fail("阈值名称-阈值值已存在");
        }
        workflowThresholdDetailService.save(sysDictDetail);
        return DataResult.success();
    }

    @ApiOperation(value = "更新")
    @PutMapping("/update")
    @RequiresPermissions("workflow:threshold:update")
    public DataResult update(@RequestBody WorkflowThresholdDetailEntity sysDictDetail) {
        if (StringUtils.isEmpty(sysDictDetail.getValue())) {
            return DataResult.fail("阈值值不能为空");
        }
        LambdaQueryWrapper<WorkflowThresholdDetailEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(WorkflowThresholdDetailEntity::getValue, sysDictDetail.getValue());
        queryWrapper.eq(WorkflowThresholdDetailEntity::getThresholdId, sysDictDetail.getThresholdId());
        WorkflowThresholdDetailEntity q = workflowThresholdDetailService.getOne(queryWrapper);
        if (q != null && !q.getId().equals(sysDictDetail.getId())) {
            return DataResult.fail("阈值名称-阈值值已存在");
        }

        workflowThresholdDetailService.updateById(sysDictDetail);
        return DataResult.success();
    }


    @SuppressWarnings({ "rawtypes", "unchecked" })
	@ApiOperation(value = "查询列表数据")
    @PostMapping("/listByPage")
    @RequiresPermissions("workflow:threshold:list")
    public DataResult findListByPage(@RequestBody WorkflowThresholdDetailEntity sysDictDetail) {
        Page page = new Page(sysDictDetail.getPage(), sysDictDetail.getLimit());
        if (StringUtils.isEmpty(sysDictDetail.getThresholdId())) {
            return DataResult.success();
        }
        IPage<WorkflowThresholdDetailEntity> iPage = workflowThresholdDetailService.listByPage(page, sysDictDetail.getThresholdId());
        return DataResult.success(iPage);
        
    }
    
    
	@ApiOperation(value = "查询列表数据")
    @PostMapping("/list")
    public DataResult list(@RequestBody WorkflowThresholdDetailEntity sysDictDetail) {
        LambdaQueryWrapper<WorkflowThresholdDetailEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(WorkflowThresholdDetailEntity::getThresholdId, sysDictDetail.getThresholdId());
        List<WorkflowThresholdDetailEntity> list = workflowThresholdDetailService.list(queryWrapper);
        return DataResult.success(list);
        
    }
}
